package com.tantan.server;

import com.tantan.model.User;

import java.io.*;
import java.util.LinkedList;
import java.util.List;

/**
 * 编程实现数据的存取
 */
public class ServerDao {

    /**
     * 自定义成员变量记录流对象
     */
    private ObjectInputStream ois;
    private ObjectOutputStream oos;
    /**
     * 自定义成员变量实现User类的容器
     */
    private List<User> studentUserList = new LinkedList<>();

    /**
     * 编程实现管理员账号和密码的校验，并将结果返回出去
     * @param user
     * @return
     */
    public boolean serverManagerCheck(User user) {
        if("admin".equals(user.getUserName()) && "123456".equals(user.getPassWord())){
            return true;
        }
        return false;
    }

    /**
     * 编程实现学员账号和密码的校验
     * @param user
     * @return
     */
    public boolean serverStudentCheck(User user) throws IOException, ClassNotFoundException {
        ois = new ObjectInputStream(new FileInputStream("C:\\fileTest\\student.txt"));
        studentUserList.addAll((LinkedList) ois.readObject());
        for (User u:studentUserList) {
            if(user.equals(u)){
                return true;
            }
        }
        return false;
    }

    /**
     * 自定义成员方法实现学员的增加
     */
    public boolean increaseStudents(User user) throws IOException, ClassNotFoundException {
        ois = new ObjectInputStream(new FileInputStream("C:\\fileTest\\student.txt"));
        studentUserList.addAll((LinkedList) ois.readObject());
        File file = new File("C:\\fileTest\\student.txt");
        for (User u:studentUserList) {
            if(user.equals(u)){
                return false;
            }
        }
        file.delete();
        oos = new ObjectOutputStream(new FileOutputStream("C:\\fileTest\\student.txt"));
        studentUserList.add(user);
        oos.writeObject(studentUserList);
        System.out.println("增加学员成功！");
        return true;
    }

    /**
     * 编程实现管理员删除学员
     */
    public boolean deleteStudents(User user) throws IOException, ClassNotFoundException {
        ois = new ObjectInputStream(new FileInputStream("C:\\fileTest\\student.txt"));
        studentUserList.addAll((LinkedList) ois.readObject());
        File file = new File("C:\\fileTest\\student.txt");
        for (User u:studentUserList) {
            if(user.equals(u)){
                file.delete();
                studentUserList.remove(u);
                oos = new ObjectOutputStream(new FileOutputStream("C:\\fileTest\\student.txt"));
                oos.writeObject(studentUserList);
                System.out.println("删除学员成功！");
                return true;
            }
        }
        return false;
    }

    /**
     * 编程实现修改学员信息
     */
    public boolean changeStudents(User user) throws IOException, ClassNotFoundException {
        ois = new ObjectInputStream(new FileInputStream("C:\\fileTest\\student.txt"));
        studentUserList.addAll((LinkedList) ois.readObject());
        File file = new File("C:\\fileTest\\student.txt");
        int i = 0;
        for (User u:studentUserList) {
            if(user.getUserName().equals(u.getUserName())){
                studentUserList.remove(u);
                u.setPassWord(user.getPassWord());
                studentUserList.add(u);
                file.delete();
                oos = new ObjectOutputStream(new FileOutputStream("C:\\fileTest\\student.txt"));
                oos.writeObject(studentUserList);
                System.out.println("修改学员密码信息成功！");
                return true;
            }
        }
        return false;
    }

    /**
     * 编程实现学员信息查找
     */
    public boolean findStudents(User user) throws IOException, ClassNotFoundException {
        ois = new ObjectInputStream(new FileInputStream("C:\\fileTest\\student.txt"));
        studentUserList.addAll((LinkedList) ois.readObject());
        for (User u:studentUserList) {
            if(user.equals(u)){
                return true;
            }
        }
        return false;
    }
}
